package com.leedsoft.service;

import com.leedsoft.common.Page;
import com.leedsoft.controller.vo.AidPageVo;
import com.leedsoft.controller.vo.PageVo;
import com.leedsoft.domain.AidOrg;
import com.leedsoft.domain.AidOrgExample;
import com.leedsoft.domain.AidOrgMemExample;
import com.leedsoft.mapper.AidOrgMapper;
import com.leedsoft.mapper.AidOrgMemMapper;
import com.leedsoft.mapper.self.AidOrgSelfMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class AidOrgServiceImpl implements AidOrgService {
    @Autowired
    private AidOrgSelfMapper aidOrgSelfMapper;
    @Autowired
    private AidOrgMapper aidOrgMapper;
    @Autowired
    private AidOrgMemMapper aidOrgMemMapper;

    @Override
    public Page<AidOrg> getOrgPlanList(PageVo aidPageVo){
        Page<AidOrg> page = new Page<>(aidPageVo);//获得page

        int count = aidOrgSelfMapper.queryAidOrgListCount(aidPageVo);
        page.setCount(count);
        if(count != 0) {
            List<AidOrg> aidOrgs = aidOrgSelfMapper.queryAidOrgList(aidPageVo);
            page.setList(aidOrgs);
        }
        return page;
    }
    @Override
    public void add(AidOrg aidOrg){
        aidOrgMapper.insertSelective(aidOrg);
    }

    @Override
    public List<AidOrg> queryList(){
        AidOrgExample orgExample = new AidOrgExample();
        List<AidOrg> list = aidOrgMapper.selectByExample(orgExample);
        return list;
    }

    @Override
    public AidOrg queryOrgById(long id){
        return aidOrgMapper.selectByPrimaryKey(id);
    }

    @Override
    public void updateAidOrg(AidOrg aidOrg){
        aidOrgMapper.updateByPrimaryKeySelective(aidOrg);
    }

    @Override
    @Transactional
    public void deleteAidOrg(long id) {
        aidOrgMapper.deleteByPrimaryKey(id);
        AidOrgMemExample aidOrgMemExample = new AidOrgMemExample();
        aidOrgMemExample.createCriteria().andOrg_idEqualTo(id);//删除机构成员中间表中的信息
        aidOrgMemMapper.deleteByExample(aidOrgMemExample);
    }
}

